package com.one.common.view.base;


import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;


import com.one.common.presenter.BasePresenter;
import com.one.common.utils.list.ListUtils;

import java.util.ArrayList;

/**
 * Author：zhulunjun
 * Time：2018/2/25
 * description：
 */

public abstract class BaseFragmentActivity<T extends BasePresenter> extends BaseActivity<T> {

    protected FragmentManager mFragmentManager = getFragmentManager();
    protected ArrayList<BaseFragment> mFragments;
    protected Fragment mCurrFragment;


    protected void setCurrFragment(Fragment targetFragment, String tag) {
        setCurrFragment(getContainerViewId(), targetFragment, tag);
    }

    /**
     * Note that: Fragments in the same type and with same container ID will not work on right,event with different
     * tags,if you has to use same type Fragments which only distinguished by Arguments,you can use ViewPager
     * instead.
     */
    protected void setCurrFragment(int containerViewId, Fragment targetFragment, String tag) {
        if (mCurrFragment == targetFragment) {
            return;
        }
        if (targetFragment.isAdded()) {
            showFragment(targetFragment);
        } else {
            addFragment(containerViewId, targetFragment, tag);
        }
        mCurrFragment = targetFragment;
    }

    /**
     * 添加Fragment
     */
    private void addFragment(int containerViewId, Fragment targetFragment, String tag) {
        FragmentTransaction commonTransaction = getCommonTransaction();
        hideOtherFragments(commonTransaction, targetFragment);
        commonTransaction.add(containerViewId, targetFragment, tag);
        commonTransaction.commitAllowingStateLoss();
    }

    /**
     * 显示传入的Fragment
     *
     * @param targetFragment 需要显示的Fragment
     */
    private void showFragment(Fragment targetFragment) {
        FragmentTransaction commonTransaction = getCommonTransaction();
        hideOtherFragments(commonTransaction, targetFragment);
        commonTransaction.show(targetFragment);
        commonTransaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏其他的Fragment
     */
    private void hideOtherFragments(FragmentTransaction commonTransaction, Fragment targetFragment) {
        if (mFragments != null && mFragments.size() > 0) {
            for (BaseFragment fragment : mFragments) {
                if (fragment != null && fragment.isAdded() && fragment != targetFragment) {
                    commonTransaction.hide(fragment);
                }
            }
        }
    }

    /**
     * 获取FragmentTransaction
     */
    protected FragmentTransaction getCommonTransaction() {
        return mFragmentManager.beginTransaction();//.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
    }

    protected abstract ArrayList<BaseFragment> getFragments();


    protected abstract int getContainerViewId();


    public Fragment getCurrFragment() {
        return mCurrFragment;
    }

    /**
     * 切换page
     * @param index 下标
     */
    public void changePage(int index){
        if(ListUtils.isEmpty(mFragments)){
            mFragments = getFragments();
        }
        setCurrFragment(mFragments.get(index),""+index);
    }

}
